<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;
use App\Models\Course;
use App\Services\CourseService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class CourseController extends AdminController
{
    //添加
    public function  add(Request $request){
        $validator = Validator::make($request->all(), [
            'course_categories_id' => 'required|integer|digits_between:1,20|min:1',
            'course_lecturer_id' => 'required|integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'pic' => 'string',
            'chapter' => 'string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $request->offsetSet('pv',ceil(mt_rand(1,10)*31));
        Course::create($request->all());
        return $this->success();
    }

    //修改
    public function  update(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'course_categories_id' => 'required|integer|digits_between:1,20|min:1',
            'course_lecturer_id' => 'required|integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'pic' => 'string',
//            'chapter' => 'string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method','_token','categorie_name','lecturer_name','lecturer_pic']);
        $course = new Course();
        $course->find($request->id)->update($putData);
        return $this->success();
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [
            'course_categories_id' => 'integer|digits_between:1,20|min:1',
            'course_lecturer_id' => 'integer|digits_between:1,20|min:1',
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
            'name' => 'string',
            'recommend' => 'integer',
            'free' => 'integer',


        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $name = $request->input('name');
        $recommend = $request->input('recommend');
        $course_categories_id  = $request->input('course_categories_id');
        $course_lecturer_id  = $request->input('course_lecturer_id');
        $free = $request->input('free');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');

        $list = CourseService::getInstance()->list($page,$limit,$sort,$order, $name,$recommend,$course_categories_id,$course_lecturer_id,$free);
        return $this->success($list);
    }
    // 上传头像
    public function upfile(Request $request) {
        $url = config('filesystems.disks.course.url');
        $default = config('filesystems.disks.course.default');
        $pic =  CourseService::getInstance()->upfile($request,$url,$default,'course');
        return $this->success([
                'pic'=>$pic,
            ]
        );
    }
    //
    public function  getByCourseInfo(Request $request){
        $validator = Validator::make($request->all(), [
            'course_id' => 'required|integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
            'name' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $course_id = $request->input('course_id');
        $list = CourseService::getInstance()->getByCourseInfo($course_id);
        return $this->success($list);
    }

    //删除
    public function  delete(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $course = Course::find($request->id);
        $course->delete();
        return $this->success();
    }

    //获取不属于某个学校课程
    public function  getNotSchoolCourse(Request $request){

        $validator = Validator::make($request->all(), [
            'school_id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $school_id = $request->input('school_id');
        $list = CourseService::getInstance()->getNotSchoolCourse($school_id);
        return $this->success($list);
    }
    //获取不属于某个学校班级课程
    public function  getNotSchoolClassCourse(Request $request){

        $validator = Validator::make($request->all(), [
            'school_id' => 'required|integer',
            'class_grades_id'=>'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $school_id = $request->input('school_id');
        $class_grades_id = $request->input('class_grades_id');
        $list = CourseService::getInstance()->getNotSchoolClassCourse($school_id,$class_grades_id);
        return $this->success($list);
    }
    //增加浏览量
    public function  addPV(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $course = Course::find($request->id);
        $pv = $course->pv+1;
        $course= new Course();
        $course->find($request->id)->update(['pv'=>$pv]);
        return $this->success();
    }
}
